package br.com.amigooo.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import br.com.amigooo.model.AccessRequest;
import br.com.amigooo.model.ActiveSession;
import br.com.amigooo.model.Establishment;
import br.com.amigooo.model.Item;
import br.com.amigooo.model.Order;
import br.com.amigooo.model.Role;
import br.com.amigooo.model.User;
import br.com.amigooo.repository.EstablishmentImpl;
import br.com.amigooo.repository.ItemImpl;
import br.com.amigooo.repository.RoleImpl;
import br.com.amigooo.repository.UserImpl;

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	private static ServiceRegistry serviceRegistry;

	private HibernateUtil() {}

	public static SessionFactory getSessionFactory() {

		if (sessionFactory == null) {
			try {
				Configuration configuration = new Configuration();
				configuration.configure("/hibernate.cfg.xml");

				serviceRegistry = new ServiceRegistryBuilder().applySettings(
						configuration.getProperties()).buildServiceRegistry();

				configuration.addAnnotatedClass(AccessRequest.class);
				configuration.addAnnotatedClass(ActiveSession.class);
				configuration.addAnnotatedClass(Establishment.class);
				configuration.addAnnotatedClass(Item.class);
				configuration.addAnnotatedClass(Order.class);
				configuration.addAnnotatedClass(Role.class);
				configuration.addAnnotatedClass(User.class);

				sessionFactory = configuration.buildSessionFactory(serviceRegistry);
				SchemaExport se = new SchemaExport(configuration);
				se.execute(false, false, false, true);
			} catch (Throwable ex) {
				throw new ExceptionInInitializerError(ex);
			}

			return sessionFactory;

		} else {
			return sessionFactory;
		}
	}

	public static void main(String[] args) {
//		UserImpl userImpl = new UserImpl();
//		Set<User> s = new HashSet<User>();
//		s.add(userImpl.getUser("machado.rodrigom@gmail.com"));
//		EstablishmentImpl establishmentImpl = new EstablishmentImpl();
//		Establishment establishment = new Establishment();
//		establishment.setCompany("Empresa teste");
//		establishment.setEmail("machado.rodrigom@gmail.com");
//		establishment.setPhone("5555-5555");
//		establishment.setTax("00000000-0001");
//		establishment.setUser(s);
//		establishmentImpl.save(establishment);
//
//		RoleImpl roleImpl = new RoleImpl();
//		
//		User user = new User();
//		user = userImpl.getUser("machado.rodrigom@gmail.com");
//		user.setEstablishment(establishment);
//		userImpl.update(user);
//		Role role = roleImpl.getRole("ROLE_ADMIN");
//		
//		Set<Role> lRole = new HashSet<Role>();
//		lRole.add(role);
//		
//		user.setActive(true);
//		user.setEmail("machado.rodrigom@gmail.com");
//		user.setName("Rodrigo Machado");
//		user.setRating(0);
//		user.setRegistration("machado");
//		user.setRegistrationDate(new Date());
//		user.setUserPassword("adm");
//		user.setRole(lRole);
//		user.setEstablishment(establishmentImpl.getEstablishment(1));
//		
//		userImpl.save(user);
		
	}
}
